package webviewerport.cerescnt.com.cereswebviewreport;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        WebView webView = (WebView)findViewById(R.id.webView);
        webView.loadUrl("file:///android_asset/s006-Chapter-001.xhtml");

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                doSetDeviceInfoList(view);
            }
        });
    }

    private void doSetDeviceInfoList(WebView view){
        ListView listView = (ListView)findViewById(R.id.listView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);

        listView.setAdapter(adapter);

        adapter.add("WebView Width = " + view.getWidth());
        adapter.add("WebView Height = " + view.getHeight());

        adapter.add("SCALE = " + String.format("%d / %d = %5.5f",view.getWidth(),view.getHeight(),(double)((double)view.getWidth()/(double)view.getHeight())));
        adapter.add("SCALE = " + String.format("%d / %d = %5.5f",view.getHeight(),view.getWidth(),(double)((double)view.getHeight()/(double)view.getWidth())));



        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);

        float density = getResources().getDisplayMetrics().density;
        adapter.add(" density = " + density);

        adapter.add(" display width  = " + display.getWidth());
        adapter.add(" display height  = " + display.getHeight());


        adapter.add(" outMetrics.widthPixels  = " + outMetrics.widthPixels);
        adapter.add(" outMetrics.heightPixels  = " + outMetrics.heightPixels);

        adapter.add("getWindow().getDecorView()" + getWindow().getDecorView().getWidth() +":"+getWindow().getDecorView().getHeight());
        adapter.add("getWindow().getDecorView()" + getWindow().getDecorView().getRootView().getWidth() +":"+getWindow().getDecorView().getRootView().getHeight());
        adapter.add("getWindow().getDecorView()" + getWindow().getDecorView().getRootView().getMeasuredWidth() +":"+getWindow().getDecorView().getRootView().getMeasuredHeight());


        adapter.add("Build.VERSION.SDK_INT\t:" + Build.VERSION.SDK_INT);
        adapter.add("Build.VERSION.CODENAME\t:" + Build.VERSION.CODENAME);
        adapter.add("Build.DEVICE\t:" + Build.DEVICE.toString());
        adapter.add("Build.DISPLAY\t:" + Build.DISPLAY.toString());
        adapter.add("Build.MODEL\t:" + Build.MODEL.toString());


        adapter.notifyDataSetChanged();;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
